from abc import ABCMeta, abstractmethod
from pydantic import BaseModel, ValidationError, constr, Field
from typing import Optional
import logging
import json
from common import isDeplyOnAws
from config.config import AppConfig
from api.home.service import HomeService

LOGGER = logging.getLogger(__name__)


MODEL_GPT35 = "gtp35"
# MODEL_GPT40 = "gtp4"
MODEL_GEMINI10 = "gemini10"
# MODEL_GEMINI15 = "gemini14"

MODEL_QIANWEN = "qianwen"

MODEL_KIMI = "kimi"

MODEL_MOCK = "mock"


class AiQuery(BaseModel):
    userMessage: str = ""
    timeout: int = 20


class AiQueryResult(BaseModel):
    text: Optional[str] = ""
    totalToken: Optional[int] = 0
    isValid: Optional[bool] = True
    errMessage: Optional[str] = ""

    def __repr__(self) -> str:
        return json.dumps(self.dict())


class AiAdapter(object):
    __metaclass__ = ABCMeta

    @abstractmethod
    def query(self, query: AiQuery) -> AiQueryResult:
        pass


from .adapter.qianwen import Qianwen
from .adapter.mock import Mock
from .adapter.kimi import Kimi
from .adapter.gpt35 import Gpt35

from common import isDevEnv
import logging

LOGGER = logging.getLogger(__name__)


def getAdapter(modelName: str = "") -> AiAdapter:

    modelName = HomeService().getModelName()
    LOGGER.info(f"读数据库，得到模型 是 {modelName}")
    if modelName == "kimi":
        return Kimi()
    elif modelName == "qianwen":
        return Qianwen()

    raise Exception(f"请先在配置页面选择 使用的AI模型。{modelName} not found")
